<template>
  <t-link
    v-permission="['system:order:set-customer-level']"
    theme="primary"
    @click="handleOpenDialog('update', { id: props.id })"
  >
    设置客户等级
  </t-link>
  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData }"
    :api-map="{ update: setCustomerLevel }"
    header="设置客户等级"
    @request-success="handleDialogRequestSuccess"
  >
    <t-form-item label="客户等级" name="customerLevel" :rules="[{ required: true, message: '请选择客户等级' }]">
      <t-select
        v-model:value="formData.customerLevel"
        :options="dictionaryStore.get('decoration_order_customer_level')"
        placeholder="请选择客户等级"
        clearable
        filterable
        @clear="formData.customerLevel = null"
      />
    </t-form-item>
    <t-form-item
      v-if="formData.customerLevel === '4'"
      label="交房时间"
      name="handoverTime"
      :rules="[{ required: true, message: '请选择交房时间' }]"
    >
      <t-date-picker 
        allow-input
        v-model:value="formData.handoverTime"
        clearable
        default-time="23:59:59"
        enable-time-picker
        placeholder="请选择交房时间"
        style="width: 100%"
      />
    </t-form-item>
  </custom-dialog>
</template>

<script setup lang="ts">
import { useTemplateRef } from 'vue';

import { setCustomerLevel } from '@/api/order/basic';
import CustomDialog from '@/components/custom/dialog/index.vue';
import { useDictionaryStore } from '@/store';

// 字典仓库
const dictionaryStore = useDictionaryStore();
// props
const props = defineProps({
  // 订单id
  id: {
    type: Number,
    required: true,
  },
});
// emits
const emits = defineEmits(['requestSuccess']);
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);
};
</script>
